# -*- coding: utf-8 -*-
import numpy as np
import gauge
import wrf_output
# import matplotlib.pyplot as plt
import os


def Decumulation(seq):
    result = [seq[i + 1] - seq[i] for i in range(len(seq) - 1)]
    result.insert(0, seq[0])
    return result


def get_accum_band_center(path, domain, startTime, endTime):
    # domains = ['01', '02']
    # get every mp_cu group wrf_output and extract the center value
    mp = ['2', '6', '7']
    cu = ['0', '1', '2', '10']
    # split_path = path.split('/')
    # gridSpacing = split_path[-2]
    # scenario = ''
    # if gridSpacing == '139':
    #     scenario = r'Scenario 1'
    # elif gridSpacing == '51545':
    #     scenario = r'Scenario 2'
    # elif gridSpacing == '103090':
    #     scenario = r'Scenario 3'

    results = []
    for i in mp:
        for j in cu:
            mp_cu = i + j
            if os.path.exists(path + mp_cu + '/'):
                input = path + mp_cu + '/wrfout_d' + domain + '_' + startTime
                center = wrf_output.GetCenter(input)
                results.append(center)
    results = np.asarray(results)
    min = np.min(results, axis=0)
    max = np.max(results, axis=0)
    band = [min, max]
    # band_mean = np.mean(band, axis=0)

    # get the gauge value of the center
    gauges_center = gauge.Get50GaugeCenterAccum(startTime, endTime)

    return band, gauges_center
    # time_series = gauges_center.index
    # plot the band and the gauge value
    # labels = ('band', 'gague', 'average')
    # plt.figure(figsize=(10, 10))
    # plt.fill_between(time_series, band[0], band[1], facecolor='blue',
    #                  alpha=0.3, label=labels[0])
    # plt.plot(time_series, gauges_center, 'r-', label=labels[1])
    # plt.plot(time_series, band_mean, 'g--', label=labels[2])
    # # plt.legend(loc='best', fontsize='xx-large')
    # plt.xlabel('time', fontdict={'size': 24})
    # plt.ylabel('precipitation', fontdict={'size': 24})
    # for x_label in plt.gca().xaxis.get_ticklabels():
    #     x_label.set_fontsize(20)
    # for y_label in plt.gca().yaxis.get_ticklabels():
    #     y_label.set_fontsize(20)
    # plt.title(startTime + '~' + endTime + ' ' + scenario, fontsize=28)
    # plt.show()


if __name__ == "__main__":
    print("get accumulated uncertainty band and center value of gauges")
